import sys
import time
import sqlite3
import os
import threading
# import xlrd
import numpy as np
import matplotlib.pyplot as plt  # plt 用于显示图片
import matplotlib.image as mpimg  # mpimg 用于读取图片
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtSql import QSqlDatabase, QSqlQuery

# 界面
from open import *
from open1 import *
from information import *
from bingli import *
from pain import *
from anmo import *
from yaobei import *
from detail import *
from check import  *
# from yanshianmo import * # 测试实时显示用

import pic_rc  # pyrcc5 -o pic.py  pic.qrc

# 弹窗
from w3finish import *
from w3finish1 import *
from w3pause1 import *
from comment import *


# /********----------------------------界面槽函数---------------------------------------------------********/
# 开始界面
class MyWindow0(QMainWindow, Ui_open):
    def __init__(self, parent=None):
        super(MyWindow0, self).__init__(parent)
        self.setupUi(self)  # 这三行必须要有，ui——open载入open界面，（Qt生成open.ui文件，通过工具转成open.py文件）


# 用户基本信息录入界面
class MyWindow1(QMainWindow, Ui_open1):
    def __init__(self, parent=None):
        super(MyWindow1, self).__init__(parent)
        self.setupUi(self)

    def open(self):  # 可有可无，用show替代即可
        self.show()


# 新用户信息录入
class MyWindow2(QMainWindow, Ui_information):
    def __init__(self, parent=None):
        super(MyWindow2, self).__init__(parent)  # 实例化类
        self.setupUi(self)  # 相当于设置控件

    def open(self):  # 被调用的类需要再编写一个open函数
        self.show()

    def Savedata(self):  # 存储用户基本信息，包含姓名、性别（女1男2）、年龄、联系方式，数据库方式
        db_file = "./infodata.db"
        conn = sqlite3.connect(db_file)
        cur = conn.cursor()
        sql = "insert into userdata('name','gender','age','phone') values(?,?,?,?)"
        a = self.Gender.currentIndex()
        data = (self.Name.text(), a, self.Age.text(), self.PhoneNumber.text())
        print(data)
        cur.execute(sql, data)
        conn.commit()
        cur.close()
        conn.close()
        return True


# 病例录入界面
class MyWindow3(QMainWindow, Ui_bingli):
    def __init__(self, parent=None):
        super(MyWindow3, self).__init__(parent)
        self.setupUi(self)

    def open(self):
        self.show()

    def nameshow(self):  # 数据库相关
        db_file = "./infodata.db"
        conn = sqlite3.connect(db_file)
        cur = conn.cursor()
        # cur.execute("delete from userdata") #删除数据
        cur.execute("select name from userdata LIMIT 1 offset (select count(*) - 1 from userdata)")  # 显示最后一条的信息
        # cur.execute("select name from userdata where id=1")
        # values = cur.fetchall()#二维数据组
        values = cur.fetchone()
        for f in values:
            value = f
            self.Namelabel.setText(value)
        conn.commit()
        cur.close()
        conn.close()

# 系统自检界面
class MyWindow4(QMainWindow, Ui_system_check):
    def __init__(self, parent=None):
        super(MyWindow3, self).__init__(parent)
        self.setupUi(self)

    def open(self):
        self.show()






# 按摩模式选择界面
class MyWindow5(QMainWindow, Ui_anmo):
    def __init__(self, parent=None):
        super(MyWindow4, self).__init__(parent)
        self.setupUi(self)  # 相当于设置控件

    def open(self):
        self.show()

    def tuntuianmo(self):
        os.system("python tuntuianmo.py")  # 执行一个程序 （！！！！！！！！！！！待修改执行位置）


# 按摩主界面
class MyWindow6(QMainWindow, Ui_yaobei):
    def __init__(self, parent=None):
        super(MyWindow5, self).__init__(parent)
        self.setupUi(self)

    def open(self):
        self.show()

    def yaobeianmo1(self):  # 选择腰背按摩模式
        self.modelName.setText('腰背按摩')
        self.startButton.clicked.connect(self.yaobeianmo)

    def tuntuianmo1(self):  # 选择臀腿按摩模式
        self.modelName.setText('臀腿按摩')
        self.startButton.clicked.connect(self.tuntuianmo)

    def yanshianmo1(self):  # 选择演示按摩模式
        self.modelName.setText('演示按摩')
        self.startButton.clicked.connect(self.yanshianmo)

    def yaobeianmo(self):  # 腰背按摩
        from yaobeianmo import MyWindow3
        MyWindow3.wdl(self)

    def tuntuianmo(self):
        from tuntuianmo import MyWindow3
        MyWindow3.wdl(self)  # 臀腿按摩

    def yanshianmo(self):  # 腰背按摩
        from yanshianmo import MyWindow3
        MyWindow3.wdl(self)

    def comment(self):  # 结束按钮对应弹窗
        class comment(QDialog, Ui_comment):
            def __init__(self, parent=None):
                super(comment, self).__init__()  # 实例化类
                self.setupUi(self)  # 相当于设置控件

        dialog = comment()
        dialog.pushButton.clicked.connect(dialog.close)
        dialog.pushButton.clicked.connect(self.close)
        dialog.exec()

    def showfinishdialog(self):
        class w3finish1(QDialog, Ui_w3finish1):
            def __init__(self, parent=None):
                super(w3finish1, self).__init__()  # 实例化类
                self.setupUi(self)  # 相当于设置控件

        dialog = w3finish1()
        dialog.pushButton_2.clicked.connect(dialog.close)
        dialog.pushButton.clicked.connect(dialog.close)
        dialog.pushButton.clicked.connect(self.close2)
        dialog.exec()

    def showpausedialog(self):  # 结束按钮对应弹窗
        class w3pause1(QDialog, Ui_Dialog):
            def __init__(self, parent=None):
                super(w3pause1, self).__init__()  # 实例化类
                self.setupUi(self)  # 相当于设置控件

        dialog = w3pause1()
        dialog.pushButton.clicked.connect(dialog.close)
        dialog.pushButton_2.clicked.connect(dialog.close)
        dialog.pushButton_2.clicked.connect(self.showfinishdialog)
        dialog.exec()

    def close2(self):
        self.close()
    # sys.exit()
    # QCoreApplication.instance().quit()


# 后台监控窗口
class MyWindow7(QMainWindow, Ui_detail):
    def __init__(self, parent=None):
        super(MyWindow6, self).__init__()
        self.setupUi(self)

    def open(self):
        self.show()

# 疼痛阈值采集界面
class MyWindow8(QMainWindow, Ui_pain):
    def __init__(self, parent=None):
        super(MyWindow7, self).__init__()  # 实例化类
        self.setupUi(self)  # 相当于设置控件

    def open(self):
        self.show()

    def nameshow(self):
        db_file = "./infodata.db"
        conn = sqlite3.connect(db_file)
        cur = conn.cursor()
        cur.execute("select name from userdata LIMIT 1 offset (select count(*) - 1 from userdata)")  # 显示最后一条的信息
        values = cur.fetchone()
        for f in values:
            value = f
            self.Namelabel.setText(value)  # 放置文字
        conn.commit()
        cur.close()
        conn.close()

# /***********------------------------主函数---------------------------------------------********/
if __name__ == '__main__':
    app = QApplication(sys.argv)
    # 实例化各个类
    w0 = MyWindow0()  # 封面
    w1 = MyWindow1()  # 用户基本信息录入
    w2 = MyWindow2()  # 新用户录入
    w3 = MyWindow3()  # 病例录入
    w4 = MyWindow4()  # 系统自检
    w5 = MyWindow5()  # 按摩模式选择界面
    w6 = MyWindow6()  # 按摩主界面
    w7 = MyWindow7()  # 后台监控窗口
    w8 = MyWindow8()  # 疼痛阈值采集界面

    # # 封面
    w0.show()
    # #用户信息录入界面
    w0.Open.clicked.connect(w1.open)  # 点击开始按钮，基本信息界面显示
    w0.Open.clicked.connect(w0.close)  # 点击开始按钮，第一个界面关闭

    # 用户信息录入界面
    w1.pushButton.clicked.connect(w2.open)  # pushButton 为按键名称，clicked——点击，connect-链接到函数
    w1.pushButton.clicked.connect(w1.close)  # close the w1
    w1.pushButton_8.clicked.connect(w0.show)
    w1.pushButton_8.clicked.connect(w1.close)

    # 病例录入界面
    w2.Save.clicked.connect(w2.Savedata)
    w2.pushButton_6.clicked.connect(w3.open)
    w2.pushButton_6.clicked.connect(w2.close)
    w2.Save.clicked.connect(w3.open)
    w2.Save.clicked.connect(w3.nameshow)
    w2.Save.clicked.connect(w2.close)
    w2.pushButton_7.clicked.connect(w4.open)
    w2.pushButton_7.clicked.connect(w2.close)
    w2.pushButton_9.clicked.connect(w2.close)
    w2.pushButton_9.clicked.connect(w1.open)

    w3.pushButton_3.clicked.connect(w2.open)
    w3.pushButton_3.clicked.connect(w3.close)
    w3.back.clicked.connect(w2.open)
    w3.pushButton_7.clicked.connect(w4.open)
    w3.pushButton_7.clicked.connect(w3.close)
    w3.StartMassage.clicked.connect(w4.open)
    w3.StartMassage.clicked.connect(w3.close)
    w3.pain.clicked.connect(w3.close)
    w3.pain.clicked.connect(w7.open)
    w3.pain.clicked.connect(w7.nameshow)

    w7.back.clicked.connect(w7.close)
    w7.back.clicked.connect(w3.open)
    w7.quxiao.clicked.connect(w7.close)
    w7.quxiao.clicked.connect(w3.open)
    w7.queren.clicked.connect(w7.close)
    w7.queren.clicked.connect(w3.open)

    w4.yaobei.clicked.connect(w5.open)
    w4.yaobei.clicked.connect(w5.yaobeianmo1)
    w4.tuntui.clicked.connect(w5.open)
    w4.tuntui.clicked.connect(w5.tuntuianmo1)
    w4.yanshi.clicked.connect(w5.open)
    w4.yanshi.clicked.connect(w5.yanshianmo1)

    # w5.startButton.clicked.connect(w5.yaobeianmo)
    w5.finishButton.clicked.connect(w5.showfinishdialog)
    w5.pauseButton.clicked.connect(w5.showpausedialog)
    w5.jiankongButton.clicked.connect(w6.show)
    w5.jiankongButton.clicked.connect(w5.close)
    # w4.tuntui.clicked.connect(w4.close)

    w6.moshi.clicked.connect(w6.close)
    w6.moshi.clicked.connect(w5.show)

    w4.pushButton_3.clicked.connect(w2.open)  # 个人信息
    w4.pushButton_3.clicked.connect(w4.close)
    w4.pushButton_6.clicked.connect(w3.open)  # 病例
    w4.pushButton_6.clicked.connect(w4.close)
    w4.back.clicked.connect(w3.open)
    w4.back.clicked.connect(w4.close)

    sys.exit(app.exec_())
